import { dirname } from 'node:path';
import { getPackages as getPackagesFunc } from '@manypkg/get-packages';
import { findUpSync } from 'find-up';

function findMonorepoRoot(cwd: string = process.cwd()) {
    const lockFile = findUpSync('pnpm-lock.yaml', {
        cwd,
        type: 'file'
    });
    return dirname(lockFile || '');
}

async function getPackages() {
    const root = findMonorepoRoot();

    return await getPackagesFunc(root);
}

export { findMonorepoRoot, getPackages };
